function O(_id){
	return document.getElementById(_id);
}
//改变select选中状态
function setSelectChecked(selectId, checkValue){  
    var select = O(selectId);  
    for(var i=0; i<select.options.length; i++){  
        if(select.options[i].innerHTML == checkValue){  
            select.options[i].selected = true;  
            break;  
        }  
    }  
}; 

/* DataTable部分
----------------------------------------------------------*/
function createDatatable(_id){
    $(_id).DataTable({
            responsive: true,
            ordering: false,
            "bRetrieve": true,
            "bFilter": true,//过滤功能
            "aLengthMenu": [[5, 10, 50, -1], [5, 10, 50, "All"]],
            "iDisplayLength":10,
            //searching: false,
            "bLengthChange": false
    });
}

function getAllNodes(_id)
{
	return $(_id).dataTable().fnGetNodes();
}
$(document).ready
(
	function()
	{
		decorateTd("loc_1");
	}
)

var _select;
var _selectteacher;
var _selectclassroom;
  //单条信息设置
            function setOne(obj){
            	_select = "#select1 "
            	_selectteacher="setTer";
				_selectclassroom = "#setLoc";
				var tr = obj.parentNode.parentNode;
            	var testNo = obj.getAttribute("testNo");
            	var tNo = obj.getAttribute("tNo");
            	var time=tr.cells[5].innerHTML;
            	var loc=tr.cells[6].innerHTML;
            	var ter=tr.cells[7].innerHTML;
            	$("#setTer").html(ter);
            	$("#setTer").attr("tNo",tNo);
            	$("#setLoc").html(loc);
            	$("#setTime").val(time);
            	$("#set_conf").attr("testNo",testNo);
            	tDict = new Object();
            	//alert(testNo);
            	show("#mask,#above-mask,#setOne_pannal");
        		$('#select1').html($('#teacher,#location').clone(true));
        		
            	
            }

//统一设置初始化
$("#setAll").on("click",function(){
$("#addTer").html("");
$("#addLoc").html("");
$("#addTime").val("");
tDict = new Object();
_select = "#select "
_selectteacher="addTer";
_selectclassroom = "#addLoc";
$('#select').html($('#teacher,#location').clone(true));
show("#mask,#above-mask,#set_pannal");
checked();

})
/* 给所有的radio加上onclick属性*/
function decorateTd( _id)
{
	var allNodes = getAllNodes(_id)
	for (var tr in allNodes)
	{
		checkbox = allNodes[tr]["cells"][0]["childNodes"][0];
		checkbox["onclick"] = function(){ unCheckHiddenCheckbox( _id ) }
	}
}
/* 使隐藏的页面的radio取消选中状态*/
function unCheckHiddenCheckbox(_id)
{
	var oTable = $(_id).dataTable();
	var hiddenNodes = oTable.fnGetHiddenNodes();
	var checkbox;
	for (var tr in hiddenNodes)
	{
		checkbox = hiddenNodes[tr]["cells"][0]["childNodes"][0];
		checkbox["checked"] = false;
	}
}

/* 获取input(radio,checkbox)状态 */
var tDict=[];
function alertTer(obj){
	
    if(tDict[obj.value] == undefined || tDict[obj.value] == null || tDict[obj.value] == 0)
        {tDict[obj.value] = 1;//alert(tDict[obj.value]);
        tDict[obj.name] = 2;
        }
    else 
        {tDict[obj.value] = 0;//alert(tDict[obj.value]);
        tDict[obj.tNo] = 0;}
}
function getTeacher(targetid)
{
	var terList ="";
	var tNoList="";
    for(var tName in tDict){
        if(tDict[tName] == 1){
        	//alert(tName);
            terList += tName + ",";
        }
    }
    for(var tNo in tDict){
        if(tDict[tNo] == 2){
        	//alert(tName);
        	tNoList += tNo + ",";
        }
    }
    //alert(tNoList);
    terList=terList.substring(0,terList.length - 1);
    tNoList=tNoList.substring(0,tNoList.length - 1);
    $("#" + targetid).attr("tNo",tNoList);
	$("#" + targetid).html(terList);
}

/* 获取input(radio,checkbox)状态 */
function getParam( tableid, targetid)
{
	var allNodes = getAllNodes("#" + tableid);
	var innerHtml = []
	for (var tr in allNodes)
	{
		checkbox = allNodes[tr]["cells"][0]["childNodes"][0];
		if(checkbox == undefined) continue;
		if ( checkbox["checked"] == true )
		{
			innerHtml.push(checkbox.value)
		}
	}
	$("#" + targetid).val(innerHtml)
}

function getClassroom(tableid,targetid){
	//alert(tableid);
	//alert(targetid);
	var allNodes = getAllNodes(tableid);
	var innerHtml = [];
	for (var tr in allNodes)
	{
		checkbox = allNodes[tr]["cells"][0]["childNodes"][0];
		//alert(checkbox["checked"]);
		if ( checkbox["checked"] == true )
		{
			if (innerHtml.length == 0){
				innerHtml.push(checkbox.value);
			}
			else{
				innerHtml.push("," + checkbox.value);
			}
		}
	}
	//alert(innerHtml);
	$(targetid).html(innerHtml);
	
}

var stuDict;//存储学生-旧护照号是否上一门课
var stuPpn;//存储学生-新护照号是否上一门课
function newStu(){
	
	$("#addStu").html("");
 	$("#stuPassportNo").val("");
 	$("#addVisaTime").html("");
 	$('#select').html($('#student').clone(true));	
    
}   

function makeStringToDictS(str,npp){
	//alert(str);alert(npp);
    stuDict = new Object();//每节课初始化
    var stuList = str.split(",");
    for(var seq in stuList){
        if(stuList[seq] != "" && stuList[seq] != null)
            stuDict[stuList[seq]] = 1;
    }
    stuPpn = new Object();//每节课初始化
    var stuList2 = npp.split(",");
    for(var seq in stuList2){
        if(stuList2[seq] != "" && stuList2[seq] != null)
        	stuPpn[stuList2[seq]] = 1;
    }
}//储存学生

function checkChecked(tableid){
    var allNodes = getAllNodes(tableid);
    for (var tr in allNodes)
	{
		checkbox = allNodes[tr]["cells"][0]["childNodes"][0];
        if(stuDict[checkbox.value] == 1)
            checkbox["checked"] = true;
	}
}
/* select全选和全部取消*/
function checkAll(obj, tableid){
    var flag = obj.checked;
    var allNodes = getAllNodes(tableid);
    for (var tr in allNodes)
	{
		checkbox = allNodes[tr]["cells"][0]["childNodes"][0];
        if(checkbox["checked"] != flag){
            checkbox["checked"] = flag;
            alterStu(checkbox);
        }
	}
}
    function alterStu(obj){
        if(stuDict[obj.value] == undefined || stuDict[obj.value] == null || stuDict[obj.value] == 0)
            {stuDict[obj.value] = 1;}
        else 
            {stuDict[obj.value] = 0;}
        var newPpn = obj.getAttribute("newPpn");
        if(stuPpn[newPpn] == undefined || stuPpn[newPpn] == null || stuPpn[newPpn] == 0)
        	stuPpn[newPpn] = 1;
        else 
        	stuPpn[newPpn] = 0;
    }
    
    function fromDictToString(targetid,stuPassportNo){
    	//alert(targetid);
    	//alert(stuPassportNo);
        var stuList = new String();
        for(var passportNo in stuDict){
            if(stuDict[passportNo] == 1){
                stuList += passportNo + ",";
            }
        }
        stuList = stuList.substring(0,stuList.length - 1);
        $(stuPassportNo).val(stuList);
        
        var stuList2 = new String();
        for(var passportNo2 in stuPpn){
            if(stuPpn[passportNo2] == 1){
                stuList2 += passportNo2 + ",";
            }
        }
        stuList2 = stuList2.substring(0,stuList2.length - 1);
        $(targetid).html(stuList2);//alert(stuList2);
    }